package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.BuildingStyleAdapter;
import com.jiayang.hhotel.adapter.DividerItemDecoration;
import com.jiayang.hhotel.bean.BuildingStyle;
import com.jiayang.hhotel.listener.OnItemClickListener;
import com.yanzhenjie.recyclerview.swipe.Closeable;
import com.yanzhenjie.recyclerview.swipe.OnSwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/8 0008.
 */

public class GuestRoomSetActivity extends XActivity {
    public static final int BUILD_FLOOR_ACTIVITY = 335;//楼层
    public static final int ORIENTATION_ACTIVITY = 336;//朝向
    public static final int REGION_ACTIVITY = 337;//区域
    public static final int CHARACTER_ACTIVITY = 338;//特性
    public static final int ROOM_LEVEL_ALL_ACTIVITY = 339;//客房等级汇总
    public static final int ROOM_LEVEL_ACTIVITY = 340;//客房等级
    public static final int ROOM_ACTIVITY = 341;//客房
    public static final int SPECIAL_ROOM_ACTIVITY = 342;//特殊房态原因
    public static final int ROOM_SERVICE_LEVEL_ACTIVITY = 343;//客房服务工作级别
    public static final int ROOM_SUMMARY_ALL_ACTIVITY = 344;//客房消费类型汇总
    public static final int ROOM_SUMMARY_ACTIVITY = 345;//消费类型
    public static final int SUMMARY_ITEM_ACTIVITY = 346;//消费项目
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private BuildingStyleAdapter mMenuAdapter;

    private List<BuildingStyle> mDataList =new ArrayList<>();

    private SwipeMenuRecyclerView mSwipeMenuRecyclerView;
    private int size = 50;

    BuildingStyle buildingStyle1,buildingStyle2,buildingStyle3,buildingStyle4,buildingStyle5,buildingStyle6,buildingStyle7,buildingStyle8,buildingStyle9,buildingStyle10,buildingStyle11,buildingStyle12;
    @Override
    public void initData(Bundle savedInstanceState) {
        init();
        initRecyclerView();
        Intent intent=getIntent();
        switch (intent.getIntExtra("type",0)){
            case BUILD_FLOOR_ACTIVITY:
                initDatas(1);
                break;
            case ORIENTATION_ACTIVITY:
                initDatas(2);
                break;
            case REGION_ACTIVITY:
                break;
            case CHARACTER_ACTIVITY:
                break;
            case ROOM_LEVEL_ALL_ACTIVITY:
                break;
            case ROOM_LEVEL_ACTIVITY:
                break;
            case ROOM_ACTIVITY:
                break;
            case SPECIAL_ROOM_ACTIVITY:
                break;
            case ROOM_SERVICE_LEVEL_ACTIVITY:
                break;
            case ROOM_SUMMARY_ALL_ACTIVITY:
                break;
            case ROOM_SUMMARY_ACTIVITY:
                break;
            case SUMMARY_ITEM_ACTIVITY:
                break;

        }
    }
    private void init(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;



    }
    private void initRecyclerView(){

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
        mSwipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
        mSwipeMenuRecyclerView = (SwipeMenuRecyclerView) findViewById(R.id.recycler_view);
        mSwipeMenuRecyclerView.setLayoutManager(new LinearLayoutManager(this));// 布局管理器。
        mSwipeMenuRecyclerView.setHasFixedSize(true);// 如果Item够简单，高度是确定的，打开FixSize将提高性能。
        mSwipeMenuRecyclerView.setItemAnimator(new DefaultItemAnimator());// 设置Item默认动画，加也行，不加也行。
        mSwipeMenuRecyclerView.addItemDecoration(new DividerItemDecoration(this,6,getResources().getColor(R.color.colorstyle)));// 添加分割线。
        // 添加滚动监听。
        mSwipeMenuRecyclerView.addOnScrollListener(mOnScrollListener);

        // 为SwipeRecyclerView的Item创建菜单就两句话，不错就是这么简单：
        // 设置菜单创建器。
        mSwipeMenuRecyclerView.setSwipeMenuCreator(swipeMenuCreator);
        // 设置菜单Item点击监听。
        mSwipeMenuRecyclerView.setSwipeMenuItemClickListener(menuItemClickListener);

        mMenuAdapter = new BuildingStyleAdapter(mDataList,getApplicationContext());
        mMenuAdapter.setOnItemClickListener(onItemClickListener);
        mSwipeMenuRecyclerView.setAdapter(mMenuAdapter);
    }
    /**
     * 刷新监听。
     */
    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mSwipeMenuRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }, 2000);
        }
    };
    /**
     * 加载更多
     */
    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                // TODO 这里有个注意的地方，如果你刚进来时没有数据，但是设置了适配器，这个时候就会触发加载更多，需要开发者判断下是否有数据，如果有数据才去加载更多。

                Toast.makeText(context, "滑到最底部了，去加载更多吧！", Toast.LENGTH_SHORT).show();
                size += 10;
                for (int i = size - 10; i < size; i++) {
                    mDataList.add(buildingStyle1);
                }
                mMenuAdapter.notifyDataSetChanged();
            }
        }
    };
    /**
     * 菜单创建器。在Item要创建菜单的时候调用。
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.item_height);


            int height = ViewGroup.LayoutParams.MATCH_PARENT;

            {
                SwipeMenuItem deleteItem = new SwipeMenuItem(context)
                        .setBackgroundDrawable(R.drawable.selector_red)
                        .setImage(R.mipmap.ic_action_delete)
                        .setText("删除") // 文字，还可以设置文字颜色，大小等。。
                        .setTextColor(Color.WHITE)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。


            }
        }
    };
    private OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            Toast.makeText(context, "我是第" + position + "条。", Toast.LENGTH_SHORT).show();
        }
    };
    /**
     * 菜单点击监听。
     */
    private OnSwipeMenuItemClickListener menuItemClickListener = new OnSwipeMenuItemClickListener() {
        /**
         * Item的菜单被点击的时候调用。
         * @param closeable       closeable. 用来关闭菜单。
         * @param adapterPosition adapterPosition. 这个菜单所在的item在Adapter中position。
         * @param menuPosition    menuPosition. 这个菜单的position。比如你为某个Item创建了2个MenuItem，那么这个position可能是是 0、1，
         * @param direction       如果是左侧菜单，值是：SwipeMenuRecyclerView#LEFT_DIRECTION，如果是右侧菜单，值是：SwipeMenuRecyclerView
         *                        #RIGHT_DIRECTION.
         */
        @Override
        public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, int direction) {
            closeable.smoothCloseMenu();// 关闭被点击的菜单。

            if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                Toast.makeText(context, "list第" + adapterPosition + "; 右侧菜单第" + menuPosition, Toast.LENGTH_SHORT).show();
            } else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION) {
                Toast.makeText(context, "list第" + adapterPosition + "; 左侧菜单第" + menuPosition, Toast.LENGTH_SHORT).show();
            }

            // TODO 推荐调用Adapter.notifyItemRemoved(position)，也可以Adapter.notifyDataSetChanged();
            if (menuPosition == 0) {// 删除按钮被点击。
                mDataList.remove(adapterPosition);
                mMenuAdapter.notifyItemRemoved(adapterPosition);
            }
        }
    };
    @Override
    public int getLayoutId() {
        return R.layout.activity_guest_romm_set;
    }

    @Override
    public Object newP() {
        return null;
    }
    public void initDatas(int type){

        buildingStyle1=new BuildingStyle(1,"A","中国","China");
        buildingStyle2=new BuildingStyle(1,"A","中国","China");
        buildingStyle3=new BuildingStyle(1,"B","中国","China");
        buildingStyle4=new BuildingStyle(1,"C","中国","China");
        buildingStyle5=new BuildingStyle(1,"D","中国","China");
        buildingStyle6=new BuildingStyle(1,"E","中国","China");
        buildingStyle7=new BuildingStyle(1,"A","美国","USA");
        buildingStyle8=new BuildingStyle(1,"A","美国","USA");
        buildingStyle9=new BuildingStyle(1,"B","美国","USA");
        buildingStyle10=new BuildingStyle(1,"C","美国","USA");
        buildingStyle11=new BuildingStyle(1,"D","美国","USA");
        buildingStyle12=new BuildingStyle(1,"E","美国","USA");
        if(type==1) {
            mDataList.add(buildingStyle6);
            mDataList.add(buildingStyle2);
            mDataList.add(buildingStyle3);
            mDataList.add(buildingStyle1);
            mDataList.add(buildingStyle2);
            mDataList.add(buildingStyle3);
            mDataList.add(buildingStyle4);
            mDataList.add(buildingStyle5);
        }else{
            mDataList.add(buildingStyle7);
            mDataList.add(buildingStyle8);
            mDataList.add(buildingStyle9);
            mDataList.add(buildingStyle10);
            mDataList.add(buildingStyle11);
            mDataList.add(buildingStyle12);
            mDataList.add(buildingStyle12);
            mDataList.add(buildingStyle11);
        }
    }
}
